package com.lxt.controller;

import com.lxt.common.HttpResult;
import com.lxt.common.util.MobileEmailUtils;
import com.lxt.pojo.BO.AddressBO;
import com.lxt.pojo.UserAddress;
import com.lxt.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "地址相关", tags = {"地址相关的api接口"})
@RestController
@RequestMapping("address")
public class AddressController {

    private static final Logger logger = LoggerFactory.getLogger(AddressController.class);

    /**
     * 用户在确认订单页面，可以针对收货地址做如下操作：
     * 1.查询用户的所有收货地址列表
     * 2.新增收货地址
     * 3.删除收货地址
     * 4.修改收货地址
     * 5.设置默认地址
     */

    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "查询用户的所有收货地址列表", notes = "查询用户的所有收货地址列表", httpMethod = "POST")
    @PostMapping("/list")
    public HttpResult commentLevel(@RequestParam String userId) {
        if (userId == null) {
            logger.error("用户不存在!!!");
            return HttpResult.errorMsg("用户不存在");
        }
        List<UserAddress> userAddresses = addressService.queryAll(userId);
        return HttpResult.ok(userAddresses);
    }

    @ApiOperation(value = "用户新增地址", notes = "用户新增地址", httpMethod = "POST")
    @PostMapping("/add")
    public HttpResult add(@RequestBody AddressBO addressBO) {

        HttpResult result = checkAddress(addressBO);
        if (result.getStatus() != 200) {
            return result;
        }
        addressService.addNewUserAddress(addressBO);
        return HttpResult.ok();
    }

    @ApiOperation(value = "用户修改地址", notes = "用户修改地址", httpMethod = "POST")
    @PostMapping("/update")
    public HttpResult update(@RequestBody AddressBO addressBO) {

        if (StringUtils.isBlank(addressBO.getAddressId())) {
            return HttpResult.errorMsg("修改地址错误，addressId不能为空");
        }

        HttpResult result = checkAddress(addressBO);
        if (result.getStatus() != 200) {
            return result;
        }
        addressService.updateUserAddress(addressBO);
        return HttpResult.ok();
    }

    @ApiOperation(value = "用户删除地址", notes = "用户删除地址", httpMethod = "POST")
    @PostMapping("/delete")
    public HttpResult delete(@RequestParam String userId, @RequestParam String addressId) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {
            return HttpResult.errorMsg("");
        }

        addressService.deleteUserAddress(userId, addressId);
        return HttpResult.ok();
    }

    @ApiOperation(value = "用户设置默认地址", notes = "用户设置默认地址", httpMethod = "POST")
    @PostMapping("/setDefault")
    public HttpResult setDefault(@RequestParam String userId, @RequestParam String addressId) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {
            return HttpResult.errorMsg("");
        }

        addressService.updateDefaultAddress(userId, addressId);
        return HttpResult.ok();
    }

    private HttpResult checkAddress(AddressBO addressBO) {
        String receiver = addressBO.getReceiver();
        if (StringUtils.isBlank(receiver)) {
            return HttpResult.errorMsg("收货人不能为空");
        }
        if (receiver.length() > 12) {
            return HttpResult.errorMsg("收货人姓名不能太长");
        }

        String mobile = addressBO.getMobile();
        if (StringUtils.isBlank(mobile)) {
            return HttpResult.errorMsg("收货人手机号不能为空");
        }
        if (mobile.length() != 11) {
            return HttpResult.errorMsg("手机号长度应为11位");
        }
        boolean isMobileOK = MobileEmailUtils.checkMobileIsOk(mobile);
        if (!isMobileOK) {
            return HttpResult.errorMsg("收货人手机号格式不正确");
        }

        String province = addressBO.getProvince();
        String city = addressBO.getCity();
        String district = addressBO.getDistrict();
        String detail = addressBO.getDetail();
        if (StringUtils.isBlank(province) || StringUtils.isBlank(city) ||
                StringUtils.isBlank(district) || StringUtils.isBlank(detail)) {
            return HttpResult.errorMsg("收货地址不能为空");
        }
        return HttpResult.ok();
    }

}
